$(function(){
	/*
	 * 加载数据
	 */
	$(document).ready(function () {
		loadApi();
	});	
	/*
	 * 搜索api
	 */
	$(document).on("click","#searchApi",function(){
		loadApi();
	});
	/*
	 * 更新api
	 */
	$(document).on("click","#updateApi",function(){
		var id = $(this).attr("value");
		findApi(id);
	});
}) 

/*
 * 加载数据
 */
function loadApi(){
	var proId = $("#proId").val();
	var url = ctx + "/project/"+ proId +"/api/list";
	var condition = $("#condition").val();
	var json = {pageable:"pageable",condition:condition};
	jqList.query(url,json, function(data){
		if(data.code == 200){
			var itemsHtml = '';
        	var len = data.list.length;
			if(len == 0){
				itemsHtml = '<tr><td colspan="9">无数据...</td></tr>';
			}
			for(var i=0; i<len; i++){
        		(function(){
        			var api = data.list[i];
        			var apiDate = new Date(api.createDate).Format("yyyy-MM-dd hh:mm:ss");
        			if(null==api.moduleName) {
        				api.moduleName="无";
        			}
        			itemsHtml += '<tr>'+
        							'<td>'+(i+1)+'</td>'+
        							'<td><a href="'+ctx+'/api/'+api.id+'/request/detail" style="text-decoration:underline;">'+api.name+'</a></td>'+
        							
        							'<td>'+api.requestUrl+'</td>'+
        							'<td>'+api.requestType+'</td>'+
        							'<td>'+api.description+'</td>'+
        							'<td>'+api.moduleName+'</td>'+
        							'<td>'+api.sort+'</td>'+
        							'<td>'+apiDate+'</td>'+
        							'<td><a id="updateApi" class="btn btn-primary btn-sm" value="'+api.id+'" href="javascript:void(0);" data-toggle="modal" data-target="#updateApiModal" style="float: left;">修改</a>'+
        							'<a class="btn btn-danger btn-sm" onClick="delApi('+api.id+')" style="float: right;">删除</a>'+
        							'</td>'+
        						 '</tr>'
        			})(i);
        		}
			$('#apiList').html(itemsHtml);
		}
	});
}
/*
 * 查询更新的api
 */
function findApi(id){
	var proId = $("#proId").attr("value");
	$.ajax({
		type: 'POST',
		dataType:'json',
		url: ctx + '/project/'+ proId +'/api/findOne',
		data: {id:id},
		success:function(data){
			if(data.code == 200){
				var api = data.api;
				$("#apiId").val(api.id);
				$("#name").val(api.name);
				$("#tableName").val(api.tableName);
				$("#tableFlag").val(api.tableFlag);
				$("#separatorValue").val(api.separatorValue);
				$("#requestUrl").val(api.requestUrl);
				$("#requestType").val(api.requestType);
				$("#requestFormat").val(api.requestFormat);
				$("#responseFormat").val(api.responseFormat);
				$("#responseDataType").val(api.responseDataType);
				$("#sort").val(api.sort);
				$("#moduleName").val(api.moduleName);
				$("#description").val(api.description);
			}
		}
	})
};

/*
 * 删除api
 */
function delApi(id){
	var proId = $("#proId").attr("value"); 
	confirm_tip("确定删除？",function(flag){
		if(flag==1){
			$.ajax({
				type: 'POST',
				dataType:'json',
				url: ctx + '/project/'+ proId +'/api/delete',
				data: {id:id},
				success:function(data){
					if(data.code == 200){
						window.location.href= ctx + '/project/'+ proId +'/api';
					}
				}
			});
		}
	});
}

/*
 * 重名校验
 */
function post(id) {
	 var name = $(id).val();
	 var id1 = $(id).attr("id");
	 var proId = $("#proId").val();
	  $.ajax({
	         type:'POST',
	         url: ctx + '/project/'+ proId +'/api/checkName',
	         data: {name:name,proId:proId,id:id1},
	         success:function(data){
	        	 if(data.code==100){
	        		 $("#"+ id1 +"1").html(name + "已存在");
	        	 }else{
	        		 $("#"+ id1 +"1").html("");
	        	 }
	         }
	     }) ; 
	}
/*
 * 排序只能输入数字
 */
function checkSort(id){
	var sort = $(id).val();
	var regex = /[0-9]$/g;
	if(!regex.test(sort)){
		$(id).val("");
	}
}